|
1
|
|
|
import PollCard from './PollCard'; |
|
2
|
|
|
import {LocaleTimezone, PollState, Voter} from '../helpers/interfaces'; |
|
3
|
|
|
import {chat_v1 as chatV1} from '@googleapis/chat'; |
|
4
|
|
|
import {progressBarText} from '../helpers/vote'; |
|
5
|
|
|
|
|
6
|
|
|
export default class PollDialogCard extends PollCard { |
|
7
|
|
|
private readonly voter: Voter; |
|
8
|
|
|
private userVotes: number[] | undefined; |
|
9
|
|
|
|
|
10
|
|
|
constructor(state: PollState, timezone: LocaleTimezone, voter: Voter) { |
|
11
|
|
|
super(state, timezone); |
|
12
|
|
|
this.voter = voter; |
|
13
|
|
|
} |
|
14
|
|
|
create() { |
|
15
|
|
|
this.buildHeader(); |
|
16
|
|
|
this.buildSections(); |
|
17
|
|
|
this.buildButtons(); |
|
18
|
|
|
this.buildFooter(); |
|
19
|
|
|
this.card.name = this.getSerializedState(); |
|
20
|
|
|
return this.card; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
getUserVotes(): number[] { |
|
24
|
|
|
if (this.state.votes === undefined) { |
|
25
|
|
|
return []; |
|
26
|
|
|
} |
|
27
|
|
|
const votes = []; |
|
28
|
|
|
const voter = this.voter; |
|
29
|
|
|
for (let i = 0; i < this.state.choices.length; i++) { |
|
30
|
|
|
if (this.state.votes[i] !== undefined && this.state.votes[i].findIndex((x) => x.uid === voter.uid) > -1) { |
|
31
|
|
|
votes.push(i); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
return votes; |
|
35
|
|
|
} |
|
36
|
|
|
choice(index: number, text: string, voteCount: number, totalVotes: number): chatV1.Schema$GoogleAppsCardV1Widget { |
|
37
|
|
|
this.userVotes = this.getUserVotes(); |
|
38
|
|
|
|
|
39
|
|
|
const progressBar = progressBarText(voteCount, totalVotes); |
|
40
|
|
|
|
|
41
|
|
|
const voteSwitch: chatV1.Schema$GoogleAppsCardV1SwitchControl = { |
|
42
|
|
|
'controlType': 'SWITCH', |
|
43
|
|
|
'name': 'mySwitchControl', |
|
44
|
|
|
'value': 'myValue', |
|
45
|
|
|
'selected': this.userVotes.includes(index), |
|
46
|
|
|
'onChangeAction': { |
|
47
|
|
|
'function': 'switch_vote', |
|
48
|
|
|
'parameters': [ |
|
49
|
|
|
{ |
|
50
|
|
|
key: 'index', |
|
51
|
|
|
value: index.toString(10), |
|
52
|
|
|
}, |
|
53
|
|
|
], |
|
54
|
|
|
}, |
|
55
|
|
|
}; |
|
56
|
|
|
return { |
|
57
|
|
|
decoratedText: { |
|
58
|
|
|
'bottomLabel': `${progressBar} ${voteCount}`, |
|
59
|
|
|
'text': text, |
|
60
|
|
|
'switchControl': voteSwitch, |
|
61
|
|
|
}, |
|
62
|
|
|
}; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|